package com.un.ebs.businessObject.controller;

import com.un.ebs.core.common.KeyValueDto;
import com.un.ebs.core.enums.ColumnRefTypeEnum;
import com.un.ebs.core.enums.DataTypeEnum;
import com.un.ebs.core.enums.DataTypeLengthEnum;
import com.un.ebs.core.metadata.EnumInfo;
import com.un.ebs.core.util.PackageUtil;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.sys.dto.ColumnDto;
import com.un.ebs.sys.dto.ObjectParentAndIndexChangeDto;
import com.un.ebs.sys.enums.DisplayEnums;
import com.un.ebs.businessObject.service.DataColumnService;
import com.un.ebs.sys.domain.Column;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author aikun
 * @ClassName DataColumnController
 * @Description DataColumnController
 * @Date 2020/1/8
 */
@RestController
@RequestMapping("/datacolumn")
@Api(value = "sys-datacolumn", description = "表字段管理")
public class DataColumnController {

    @Resource
    DataColumnService dataColumnService;

    @GetMapping("list")
    @ApiOperation(value = "list", notes = "获取数据源表字段列表")
    public WebResult<List<ColumnDto>> list(@RequestParam(required = false) String tableId) {
        return WebUtils.success(dataColumnService.list(tableId));
    }

    @PostMapping("add")
    @ApiOperation(value = "add", notes = "新增数据源表字段")
    public WebResult add(@Valid @RequestBody Column column) {
        dataColumnService.add(column);
        return WebUtils.success();
    }

    @GetMapping("getEnums")
    @ApiOperation(value = "getEnums", notes = "获取引用类型")
    public WebResult<List<EnumInfo>> getEnums() {
        return WebUtils.success(PackageUtil.getEnums(ColumnRefTypeEnum.class));
    }


    @GetMapping("/getDisplayName")
    @ApiOperation(value = "getDisplayName", notes = "获取过滤字段")
    public WebResult<?> getDisplayName() {
        return WebUtils.success(DisplayEnums.getColumnNameList());
    }

    @GetMapping("getDataTypeEnums")
    @ApiOperation(value = "getDataTypeEnums", notes = "获取字段类型")
    public WebResult<List<EnumInfo>> getDataTypeEnums() {
        return WebUtils.success(PackageUtil.getEnums(DataTypeEnum.class));
    }

    @PostMapping("update")
    @ApiOperation(value = "update", notes = "修改数据源表字段")
    public WebResult update(@Valid @RequestBody Column column) {
        dataColumnService.update(column);
        return WebUtils.success();
    }

    @PostMapping("updateDesc")
    @ApiOperation(value = "updateDesc", notes = "修改列的描述")
    public WebResult updateDesc(@RequestParam String id, @RequestParam String columnDesc) {
        dataColumnService.updateDesc(id, columnDesc);
        return WebUtils.success();
    }

    @PostMapping("updateDescBatch")
    @ApiOperation(value = "updateDescBatch", notes = "修改列的描述")
    public WebResult updateDescBatch(@RequestBody List<KeyValueDto> req) {
        dataColumnService.updateDesc(req);
        return WebUtils.success();
    }

    @GetMapping("getById")
    @ApiOperation(value = "getColumnById", notes = "id获取数据源表字段对象")
    public WebResult<ColumnDto> getById(@RequestParam String id) {
        return WebUtils.success(dataColumnService.getById(id));
    }

    @GetMapping("delete")
    @ApiOperation(value = "delete", notes = "删除数据源表字段")
    public WebResult delete(@RequestParam String id) {
        dataColumnService.delete(id);
        return WebUtils.success();
    }

    @PostMapping("changeSort")
    @ApiOperation(value = "changeSort", notes = "排序")
    public WebResult changeSort(@RequestBody List<ObjectParentAndIndexChangeDto> req) {
        dataColumnService.changeSort(req);
        return WebUtils.success();
    }

    @GetMapping("getDateTypeLength")
    @ApiOperation(value = "getDateTypeLength", notes = "通过dataTypeEnum的code获取字段长度")
    public WebResult getDateType(@RequestParam Integer dataTypeCode) {
        return WebUtils.success(DataTypeLengthEnum.getDataTypeLengthByCode(dataTypeCode));
    }

}
